/*****************************************************************************
*Purpose: Estimate core results for MTO MAF-ARF Analysis
******************************************************************************/ 

*** Define list of outcomes 
global moutlist "m26_pctblack m26_pctwhite m26_povrate m26_incrnk m26_incrnk_black m28_pctblack m28_pctwhite m28_povrate m28_incrnk m28_incrnk_black m2019_pctblack m2019_pctwhite m2019_povrate m2019_incrnk m2019_incrnk_black"


*******************************************************
*** MTO estiamtes for Figure II / Tables XXII, XXIV ***
*******************************************************
capture frame drop mtomarf
frame create mtomarf str35 outcome str35 group str35 spec beta se obs piks treatpiks controlpiks 

foreach gr in all chicago black {
foreach spec in base ivpov { 
foreach mout in $moutlist {

	if "`spec'"=="base" {

	ivreghdfe `mout' (exp s8 = ra_grp_exp ra_grp_s8) if `gr'==1 & agelt13==1 [pw=f_wt_raratio98], absorb(f_svy_siterar dob_year) cluster(famid) 
	
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & ra_grp_exp==1
	local treatpik `=r(sum)'
	unique pik if e(sample)==1 & ra_grp_exp==0 
	local controlpik `=r(sum)'
	

		
	frame post mtomarf ("`mout'") ("`gr'") ("`spec'") (_b[exp]) (_se[exp]) (e(N)) (`allpik') (`treatpik') (`controlpik')  
	sum `mout' if (ra_grp_exp!=1 & ra_grp_s8!=1) & `mout'!=. & `gr'==1  & agelt13==1 
	local controlmean `=r(mean)'
	local controlsd `=r(sd)'
	local allpik `=r(N)'
	
	frame post mtomarf ("`mout'") ("`gr'") ("meansd") (`controlmean') (`controlsd') (`allpik') (`allpik') (-99999) (-99999)  

	}

	if "`spec'"=="ivpov" {

	ivreghdfe `mout' ra_grp_s8 (f_c90t_perpov_yr1= ra_grp_exp) if `gr'==1  & agelt13==1 [pw=f_wt_raratio98], absorb(f_svy_siterar dob_year) cluster(famid)
	unique pik if e(sample)==1
	
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1  
	local treatpik `=r(sum)'
	unique pik if e(sample)==1  
	local controlpik `=r(sum)'
	

		
	frame post mtomarf ("`mout'") ("`gr'") ("`spec'") (_b[f_c90t_perpov_yr1]) (_se[f_c90t_perpov_yr1]) (e(N)) (`allpik') (-99999) (-99999)  
	}
	
	}
}
}

frame change mtomarf


frame mtomarf: save "${output}/mto_mafarf_results.dta", replace
frame mtomarf: export delimited using "${output}/mto_mafarf_results.csv", replace

